package cn.tianyigps.tianyigpsgatherchecker.service;

import cn.tianyigps.tianyigpsgatherchecker.bean.AlarmModel;
import cn.tianyigps.tianyigpsgatherchecker.bean.Condition;
import cn.tianyigps.tianyigpsgatherchecker.bean.EquipmentCheckModel;
import cn.tianyigps.tianyigpsgatherchecker.bean.RuleModel;
import cn.tianyigps.tianyigpsgatherchecker.constant.RuleTypeEnum;
import cn.tianyigps.tianyigpsgatherchecker.handler.CheckRule;
import cn.tianyigps.tianyigpsgatherchecker.handler.PushAlarmData;
import cn.tianyigps.tianyigpspublic.util.MapTransformUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Service
public class AlarmOrNot {
    private static Logger LOG = LoggerFactory.getLogger(AlarmOrNot.class);
    @Autowired
    AlarmService alarmService;
    @Autowired
    CheckPushAlarm checkPushAlarm;
    @Autowired
    EquipmentCheckModelService equipmentCheckModelService;
    //返回true报警，返回false无需报警
    public Boolean check(String equId, RuleModel<Condition> ruleModel, EquipmentCheckModel equipmentCheckModel, HashSet set) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        String ruleId = ruleModel.getId();
        Condition condition = mapper.readValue(ruleModel.getCondition().toString(), Condition.class);
        int car = Integer.parseInt(condition.getCar());
        //坐标转换
        GeoJsonPoint geoJsonPoint = equipmentCheckModel.getPoint();
        Point alarmPoint = MapTransformUtils.gcj02tobd09(geoJsonPoint.getX(), geoJsonPoint.getY());

        //定位调用报警接口，设备异常聚集报警
        AlarmModel alarmModel = new AlarmModel();
        alarmModel.setRuleId(ruleModel.getId());
        alarmModel.setRuleType(ruleModel.getType());
        alarmModel.setAlarmTime(equipmentCheckModel.getLineStatusTime());
        alarmModel.setStartTime(equipmentCheckModel.getReportTime());
        //推送数据转化为百度坐标
        alarmModel.setGpsLongitude(alarmPoint.getX());
        alarmModel.setGpsLatitude(alarmPoint.getY());
        alarmModel.setEquipmentId(equipmentCheckModel.getEquipmentId());
        alarmModel.setAlarmMsg(set.size() + "");
        alarmModel.setAlarmMsg(mapper.writeValueAsString(set));
        PushAlarmData pushAlarmData = new PushAlarmData();
        //报警的时候校验是否在报警生效时间，过了生效时间，将报警数据发不过如alarm，报警需要存入 alarmService.insert(alarmModel); 不报警要检查是否报警，关闭它
        if (CheckRule.ruleCheck(ruleModel, equipmentCheckModel.getReportTime())) {
            //判断设备是否需要报警,不需要报警就检查设备是否已经报警，已经报警则需要关闭报警
            if (set.size() < car) {
                //校验设备是否已经报警 alarmEquId为空则是没有报警，不为空为报警的设备号
                String alarmEquId = checkPushAlarm.checkPushAlarm(equipmentCheckModel, ruleId, true);
                if (!StringUtils.isEmpty(alarmEquId)) {
                    LOG.info("设备：" + alarmEquId + " 关闭报警规则 ：" + ruleId);
                    //关闭报警
                    alarmModel.setEquipmentId(alarmEquId);
                    alarmModel.setStartTime(null);
                    alarmModel.setEndTime(equipmentCheckModel.getReportTime());
                    pushAlarmData.pushAlarmDataByPost(alarmModel);
                    alarmModel.setGpsLongitude(geoJsonPoint.getX());
                    alarmModel.setGpsLatitude(geoJsonPoint.getY());
                    alarmService.save(alarmModel);
                    alarmService.insert(alarmModel);
                }
                return false;
            } else {
                String alarmEquId = checkPushAlarm.checkPushAlarm(equipmentCheckModel, ruleId, false);
                if (StringUtils.isEmpty(alarmEquId)) {
                    LOG.info("设备：" + equId + " 开始报警 ：" + ruleId);
                    pushAlarmData.pushAlarmDataByPost(alarmModel);
                    //报警数据坐标推送后需要转换回坐标
                    alarmModel.setGpsLongitude(geoJsonPoint.getX());
                    alarmModel.setGpsLatitude(geoJsonPoint.getY());
                    alarmService.save(alarmModel);
                    List<String> alarmList = equipmentCheckModel.getCheckStatusModel().getGather();
                    if (StringUtils.isEmpty(alarmList)) {
                        alarmList = new ArrayList<>();
                    }
                    alarmList.add(ruleId);
                    equipmentCheckModelService.updateCheckStatus(equId, alarmList, RuleTypeEnum.GATHER.getType());
                    alarmService.insert(alarmModel);
                    return  true;
                }
                return  false;
            }

        }else {
            return false;
        }
    }


    }